package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceParam;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceRelatedTechParam;
import com.sailfish.springbootdemo.pojo.db1.TechDeviceParam;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface TechDeviceParamDao {

    /**
     * 查询所有
     *
     * @return
     */
    @Results(id = "techDeviceParamResultMap", value = {
            @Result(column = "tech_device_param_id", property = "techDeviceParamId", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_name_cn", property = "techDeviceParamNameCn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tech_device_param_name_en", property = "techDeviceParamNameEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tech_device_param_default", property = "techDeviceParamDefault", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_type_id", property = "pcbaDeviceTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_key", property = "techDeviceParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tech_device_param_fill_type", property = "techDeviceParamFillType", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_options", property = "techDeviceParamOptions", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_required", property = "isRequired", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_type_id", property = "techDeviceParamTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "bind_device_param_key", property = "bindDeviceParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "supply_tech_device_param_fill_type", property = "supplyTechDeviceParamFillType", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_placeholder", property = "techDeviceParamPlaceholder", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tech_device_param_picture", property = "techDeviceParamPicture", jdbcType = JdbcType.VARCHAR),
            @Result(column = "notes", property = "notes", jdbcType = JdbcType.VARCHAR),
            @Result(column = "actual_equal_declare", property = "actualEqualDeclare", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_related_pcba", property = "isRelatedPcba", jdbcType = JdbcType.INTEGER),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.TECH_DEVICE_PARAM + " where 1 = 1 " +
            "<if test='searchTechDeviceParamName != null and searchTechDeviceParamName != \"\" '>" +
            " and (tech_device_param_name_cn like CONCAT('%',#{searchTechDeviceParamName},'%') or tech_device_param_name_en like CONCAT('%',#{searchTechDeviceParamName},'%'))" +
            "</if>" +
            "<if test='pcbaDeviceTypeId != null and pcbaDeviceTypeId != \"\" '>" +
            " and pcba_device_type_id = #{pcbaDeviceTypeId}" +
            "</if>" +
            "<if test='techDeviceParamTypeId != null and techDeviceParamTypeId != \"\" '>" +
            " and tech_device_param_type_id = #{techDeviceParamTypeId}" +
            "</if>" +
            " order by sort" +
            "</script>")
    List<TechDeviceParam> getAllTechDeviceParam(@Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("techDeviceParamTypeId") Integer techDeviceParamTypeId, @Param("searchTechDeviceParamName") String searchTechDeviceParamName);

    /**
     * 修改参数的 actualEqualDeclare 值
     *
     * @param techDeviceParamId
     * @param actualEqualDeclare
     * @return
     */
    @Update("update " + DB1TableName.TECH_DEVICE_PARAM + " set actual_equal_declare = #{actualEqualDeclare} where tech_device_param_id = #{techDeviceParamId}")
    Integer changeActualEqualDeclare(@Param("techDeviceParamId") Integer techDeviceParamId, @Param("actualEqualDeclare") Integer actualEqualDeclare);

    /**
     * 新增
     *
     * @param techDeviceParam
     * @return
     */
    @Insert("insert into " + DB1TableName.TECH_DEVICE_PARAM + "(tech_device_param_name_cn, tech_device_param_name_en,tech_device_param_default,create_time,sort,pcba_device_type_id,status,tech_device_param_key,is_required,tech_device_param_fill_type,tech_device_param_options,tech_device_param_type_id,bind_device_param_key,supply_tech_device_param_fill_type,tech_device_param_placeholder,tech_device_param_picture,notes,actual_equal_declare,is_related_pcba) values " +
            "(#{techDeviceParam.techDeviceParamNameCn}, #{techDeviceParam.techDeviceParamNameEn},#{techDeviceParam.techDeviceParamDefault},#{techDeviceParam.createTime},#{techDeviceParam.sort},#{techDeviceParam.pcbaDeviceTypeId},#{techDeviceParam.status},#{techDeviceParam.techDeviceParamKey},#{techDeviceParam.isRequired},#{techDeviceParam.techDeviceParamFillType},#{techDeviceParam.techDeviceParamOptions},#{techDeviceParam.techDeviceParamTypeId},#{techDeviceParam.bindDeviceParamKey},#{techDeviceParam.supplyTechDeviceParamFillType},#{techDeviceParam.techDeviceParamPlaceholder},#{techDeviceParam.techDeviceParamPicture},#{techDeviceParam.notes},#{techDeviceParam.actualEqualDeclare},#{techDeviceParam.isRelatedPcba})")
    Integer addTechDeviceParam(@Param("techDeviceParam") TechDeviceParam techDeviceParam);

    /**
     * 更新
     *
     * @param techDeviceParam
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.TECH_DEVICE_PARAM +
            "<set>" +
            "<if test='techDeviceParam.techDeviceParamNameCn != null and techDeviceParam.techDeviceParamNameCn != \"\" '>" +
            " tech_device_param_name_cn = #{techDeviceParam.techDeviceParamNameCn}," +
            "</if>" +
            "<if test='techDeviceParam.techDeviceParamNameEn != null and techDeviceParam.techDeviceParamNameEn != \"\" '>" +
            " tech_device_param_name_en = #{techDeviceParam.techDeviceParamNameEn}," +
            "</if>" +
            "<if test='techDeviceParam.techDeviceParamDefault != null and techDeviceParam.techDeviceParamDefault != \"\" '>" +
            " tech_device_param_default = #{techDeviceParam.techDeviceParamDefault}," +
            "</if>" +
            "<if test='techDeviceParam.updateTime != null '>" +
            " update_time = #{techDeviceParam.updateTime}," +
            "</if>" +
            "<if test='techDeviceParam.sort != null and techDeviceParam.sort != \"\" '>" +
            " sort = #{techDeviceParam.sort}," +
            "</if>" +
            "<if test='techDeviceParam.pcbaDeviceTypeId != null and techDeviceParam.pcbaDeviceTypeId != \"\" '>" +
            " pcba_device_type_id = #{techDeviceParam.pcbaDeviceTypeId}," +
            "</if>" +
            "<if test='techDeviceParam.status != null and techDeviceParam.status != \"\" '>" +
            " status = #{techDeviceParam.status}," +
            "</if>" +
            "<if test='techDeviceParam.techDeviceParamKey != null and techDeviceParam.techDeviceParamKey != \"\" '>" +
            " tech_device_param_key = #{techDeviceParam.techDeviceParamKey}," +
            "</if>" +
            "<if test='techDeviceParam.isRequired != null and techDeviceParam.isRequired != \"\" '>" +
            " is_required = #{techDeviceParam.isRequired}," +
            "</if>" +
            "<if test='techDeviceParam.techDeviceParamFillType != null and techDeviceParam.techDeviceParamFillType != \"\" '>" +
            " tech_device_param_fill_type = #{techDeviceParam.techDeviceParamFillType}," +
            "</if>" +
            "<if test='techDeviceParam.techDeviceParamOptions != null and techDeviceParam.techDeviceParamOptions != \"\" '>" +
            " tech_device_param_options = #{techDeviceParam.techDeviceParamOptions}," +
            "</if>" +
            "<if test='techDeviceParam.techDeviceParamTypeId != null and techDeviceParam.techDeviceParamTypeId != \"\" '>" +
            " tech_device_param_type_id = #{techDeviceParam.techDeviceParamTypeId}," +
            "</if>" +
            "<if test='techDeviceParam.bindDeviceParamKey != null and techDeviceParam.bindDeviceParamKey != \"\" '>" +
            " bind_device_param_key = #{techDeviceParam.bindDeviceParamKey}," +
            "</if>" +
            "<if test='techDeviceParam.supplyTechDeviceParamFillType != null and techDeviceParam.supplyTechDeviceParamFillType != \"\" '>" +
            " supply_tech_device_param_fill_type = #{techDeviceParam.supplyTechDeviceParamFillType}," +
            "</if>" +
            "<if test='techDeviceParam.techDeviceParamPlaceholder != null '>" +
            " tech_device_param_placeholder = #{techDeviceParam.techDeviceParamPlaceholder}," +
            "</if>" +
            "<if test='techDeviceParam.techDeviceParamPicture != null '>" +
            " tech_device_param_picture = #{techDeviceParam.techDeviceParamPicture}," +
            "</if>" +
            "<if test='techDeviceParam.actualEqualDeclare != null and techDeviceParam.actualEqualDeclare != \"\" '>" +
            " actual_equal_declare = #{techDeviceParam.actualEqualDeclare}," +
            "</if>" +
            "<if test='techDeviceParam.notes != null '>" +
            " notes = #{techDeviceParam.notes}," +
            "</if>" +
            "</set>" +
            " where tech_device_param_id = #{techDeviceParam.techDeviceParamId}" +
            "</script>")
    Integer updateTechDeviceParam(@Param("techDeviceParam") TechDeviceParam techDeviceParam);

    /**
     * 根据条件查询
     *
     * @param techDeviceParam
     * @return
     */
    @Select("<script>" +
            "select * from " + DB1TableName.TECH_DEVICE_PARAM + " where 1 = 1" +
            "<if test='techDeviceParam.techDeviceParamNameCn != null and techDeviceParam.techDeviceParamNameCn != \"\" '>" +
            " and tech_device_param_name_cn = #{techDeviceParam.techDeviceParamNameCn}" +
            "</if>" +
            "<if test='techDeviceParam.techDeviceParamNameEn != null and techDeviceParam.techDeviceParamNameEn != \"\" '>" +
            " and tech_device_param_name_en = #{techDeviceParam.techDeviceParamNameEn}" +
            "</if>" +
            "<if test='techDeviceParam.pcbaDeviceTypeId != null and techDeviceParam.pcbaDeviceTypeId != \"\" '>" +
            " and pcba_device_type_id = #{techDeviceParam.pcbaDeviceTypeId}" +
            "</if>" +
            "<if test='techDeviceParam.techDeviceParamDefault != null and techDeviceParam.techDeviceParamDefault != \"\" '>" +
            " and tech_device_param_default = #{techDeviceParam.techDeviceParamDefault}" +
            "</if>" +
            "<if test='techDeviceParam.techDeviceParamKey != null and techDeviceParam.techDeviceParamKey != \"\" '>" +
            " and tech_device_param_key = #{techDeviceParam.techDeviceParamKey}" +
            "</if>" +
            "<if test='techDeviceParam.techDeviceParamId != null and techDeviceParam.techaDeviceParamId != \"\" '>" +
            " and tech_device_param_id = #{techDeviceParam.techDeviceParamId}" +
            "</if>" +
            "</script>")
    @ResultMap(value = "techDeviceParamResultMap")
    TechDeviceParam getByTechDeviceParam(@Param("techDeviceParam") TechDeviceParam techDeviceParam);

    /**
     * 删除
     *
     * @param techDeviceParamId
     * @return
     */
    @Delete("delete from " + DB1TableName.TECH_DEVICE_PARAM + " where tech_device_param_id = #{techDeviceParamId} order by sort")
    Integer deleteById(@Param("techDeviceParamId") Integer techDeviceParamId);

    /**
     * 根据 techDeviceParamTypeId 查询
     *
     * @param techDeviceParamTypeId
     * @return
     */
    @ResultMap(value = "techDeviceParamResultMap")
    @Select("select * from " + DB1TableName.TECH_DEVICE_PARAM + " where tech_device_param_type_id = #{techDeviceParamTypeId} order by sort")
    List<TechDeviceParam> getByTechDeviceParamTypeId(@Param("techDeviceParamTypeId") Integer techDeviceParamTypeId);

    /**
     * 根据key查询
     *
     * @param techDeviceParamKey
     * @return
     */
    @ResultMap(value = "techDeviceParamResultMap")
    @Select("select * from " + DB1TableName.TECH_DEVICE_PARAM + " where tech_device_param_key = #{techDeviceParamKey}")
    TechDeviceParam getTechDeviceParamByKey(@Param("techDeviceParamKey") String techDeviceParamKey);

    /**
     * 根据绑定key查询
     */
    @ResultMap(value = "techDeviceParamResultMap")
    @Select("select * from " + DB1TableName.TECH_DEVICE_PARAM + " where bind_device_param_key like CONCAT('%',#{bindDeviceParamKey},'%') ")
    List<TechDeviceParam> getTechDeviceParamByBindKey(@Param("bindDeviceParamKey") String bindDeviceParamKey);

    /**
     * 根据 techDeviceParamTypeId 查询
     *
     * @param techDeviceParamId
     * @return
     */
    @ResultMap(value = "techDeviceParamResultMap")
    @Select("select * from " + DB1TableName.TECH_DEVICE_PARAM + " where tech_device_param_id = #{techDeviceParamId}")
    TechDeviceParam getTechDeviceParamById(@Param("techDeviceParamId") Integer techDeviceParamId);

    @ResultMap(value = "techDeviceParamResultMap")
    @Select("select * from " + DB1TableName.TECH_DEVICE_PARAM + " where tech_device_param_type_id = #{techDeviceParamTypeId} and tech_device_param_name_cn = #{techDeviceParamNameCn}")
    TechDeviceParam getByNameAndTechDeviceParamTypeId(@Param("techDeviceParamTypeId") Integer techDeviceParamTypeId, @Param("techDeviceParamNameCn") String techDeviceParamNameCn);

    /**
     * 更新是否关联pcba能力判断
     *
     * @return
     */
    @Update("update " + DB1TableName.TECH_DEVICE_PARAM + " set is_related_pcba = #{isRelatedPcba} where tech_device_param_id = #{techDeviceParamId}")
    Integer updateIsRelatedPcba(@Param("isRelatedPcba") Integer isRelatedPcba, @Param("techDeviceParamId") Integer techDeviceParamId);

    /**
     * 判断有没有重复的
     *
     * @return
     */
    @ResultMap(value = "techDeviceParamResultMap")
    @Select("<script>" +
            "select * from " + DB1TableName.TECH_DEVICE_PARAM + " where tech_device_param_name_cn = #{techDeviceParamNameCn} and pcba_device_type_id = #{pcbaDeviceTypeId} and tech_device_param_type_id = #{techDeviceParamTypeId} " +
            "<if test='techDeviceParamId != null and techDeviceParamId != \"\" '>" +
            " and tech_device_param_id != #{techDeviceParamId}" +
            "</if>" +
            "</script>")
    List<TechDeviceParam> judgeUniqueTechDeviceParam(@Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("techDeviceParamTypeId") Integer techDeviceParamTypeId, @Param("techDeviceParamId") Integer techDeviceParamId, @Param("techDeviceParamNameCn") String techDeviceParamNameCn);

    @ResultMap(value = "techDeviceParamResultMap")
    @Select("select * from " + DB1TableName.TECH_DEVICE_PARAM + " where tech_device_param_name_cn = #{techDeviceParamNameCn} and pcba_device_type_id = #{pcbaDeviceTypeId} and tech_device_param_type_id = #{techDeviceParamTypeId}")
    TechDeviceParam getTechDeviceParamByName(@Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("techDeviceParamTypeId") Integer techDeviceParamTypeId, @Param("techDeviceParamNameCn") String techDeviceParamNameCn);
}
